/*
 * Created on 19.02.2003
 */
package com.juitar.base.fingering;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.juitar.base.Chord;
import com.juitar.base.Fingering;
import com.juitar.base.Tuning;
import com.juitar.base.enumerator.ArrayFretEnumeratorFactory;
import com.juitar.base.enumerator.BitFretEnumeratorFactory;
import com.juitar.base.enumerator.FingeringEnumerator;
import com.juitar.base.enumerator.IFretEnumerator;
import com.juitar.base.enumerator.IFingeringEnumeratorFactory;

/**
 * @author SKopylov
 */

public class FingeringBuilder {

    /**
	 * Method create
	 * 
	 * @param chord
	 * @param tuning
	 * @param filter
	 * @return List of fingerings
	 * @deprecated
	 */
	public static List<Fingering> create(Chord chord, Tuning tuning, IFingeringFilter filter) {

	    IFingeringEnumeratorFactory factory = new BitFretEnumeratorFactory(); 
        FingeringEnumerator fingEnum = factory.create(chord, tuning);
		return createFingeringList(chord, tuning, filter, fingEnum);
	}

	/**
	 * 
	 * @param chord
	 * @param tuning
	 * @param filter
	 * @param fingEnum
	 * @return
	 * @deprecated
	 */
	static List<Fingering> createFingeringList(Chord chord, Tuning tuning, IFingeringFilter filter,
			FingeringEnumerator fingEnum) {

		ArrayList<Fingering> list = new ArrayList<Fingering>();
		int totalFingerings = 0;
		// int totalLongFingerings = 0;
		int totalNotChords = 0;
		int totalFiltered = 0;

		do {
			totalFingerings++;

			int[] fing = fingEnum.get();
			Fingering f = new Fingering(fing, chord, tuning);

			if (false == f.isChord()) {
				totalNotChords++;
				continue;
			}

			if (filter != null && filter.accept(f) == false) {
				totalFiltered++;
				continue;
			}
			list.add(f);

		} while (fingEnum.hasNext());

		Collections.sort(list, fingeringComparator);
		return list;
	}


	private static FingeringComparator fingeringComparator = new FingeringComparator();
}
